<?php

namespace app\modules\api\controllers;

use app\models\ApiException;
use app\models\UserPackagePool;

/**
 * 套餐
 * Class PackageController
 * @package app\modules\api\controllers
 */
class PackageController extends BaseController
{
    /**
     * 流量池信息
     * 输入
     * {
     *     month
     * }
     * 输出
     * {
     *     pool_list: [
     *         {id, name, data, count, used},
     *         ...
     *     ]
     * }
     * @throws ApiException
     */
    public function actionPool()
    {
        $user = $this->loginUser('user');

        $poolList = [];
        /** @var UserPackagePool $pool */
        foreach (UserPackagePool::find()
            ->andWhere(['uid' => $user->id, 'month' => date('Ym')])
            ->each() as $pool) {
            $poolList[] = [
                'id' => $pool->package_id,
                'name' => $pool->package->name . '流量池',
                'data' => $pool->data_pool,
                'count' => $pool->getCardCount(),
                'used' => $pool->data_used,
            ];
        }

        return [
            'pool_list' => $poolList,
        ];
    }
}
